import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
//学生课程表结合
public class Zuoye2_2 {
    public static Connection conn = null;
    public static TableName tName = TableName.valueOf("t_student_course");
    public static TableName tStudent = TableName.valueOf("t_student");
    public static TableName tCourse = TableName.valueOf("t_course");
    public static Random ra = new Random();
    @Before
    public void init() throws IOException{
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hbase:2181,hbase1:2181,hbase2:2181");
        conn = ConnectionFactory.createConnection(conf);
    }
    @Test
    public void create() throws IOException{
        Admin admin = conn.getAdmin();
        if(admin.tableExists(tName)){
            admin.disableTable(tName);
            admin.deleteTable(tName);
        }
        HTableDescriptor ht = new HTableDescriptor(tName);
        HColumnDescriptor hc = new HColumnDescriptor("cf1".getBytes());
        hc.setMaxVersions(5);
        hc.setBlockCacheEnabled(true);
        hc.setBlocksize(180000);

        ht.addFamily(hc);
        admin.createTable(ht);
        System.out.println("表创建完成");
    }

    @Test
    public void insert() throws IOException{
        Table table = conn.getTable(tName);
        List<Put> putList = new ArrayList<Put>();
        //学生1  student2016030421164483174
        String stu1 = "student2016030421164483174";
        //学生2  student2016030421164417190
        String stu2 = "student2016030421164417190";
        //学生3  student2016030421164462988
        String stu3 = "student2016030421164462988";
        //课程1 2 3 course2016030421165195800   course201603042116517244   course2016030421165117240
        Put put1 = new Put("student2016030421164483174_course2016030421165195800".getBytes());
        put1.addColumn("cf1".getBytes(), "student".getBytes(), stu1.getBytes());
        Put put2 = new Put("student2016030421164483174_course201603042116517244".getBytes());
        put2.addColumn("cf1".getBytes(), "student".getBytes(), stu1.getBytes());
        Put put3 = new Put("student2016030421164483174_course2016030421165117240".getBytes());
        put3.addColumn("cf1".getBytes(), "student".getBytes(), stu1.getBytes());
        Put put4 = new Put("student2016030421164417190_course2016030421165195800".getBytes());
        put4.addColumn("cf1".getBytes(), "student".getBytes(), stu2.getBytes());
        Put put5 = new Put("student2016030421164417190_course201603042116517244".getBytes());
        put5.addColumn("cf1".getBytes(), "student".getBytes(), stu2.getBytes());
        Put put6 = new Put("student2016030421164462988_course2016030421165195800".getBytes());
        put6.addColumn("cf1".getBytes(), "student".getBytes(), stu3.getBytes());
        putList.add(put1);
        putList.add(put2);
        putList.add(put3);
        putList.add(put4);
        putList.add(put5);
        putList.add(put6);
        table.put(putList);
        System.out.println("数据插入完成");
    }

    //查询某个学生所选的所有课程列表 学生1
    @Test
    public void findCourseByStudent() throws IOException{
        Table table = conn.getTable(tName);
        String stuId="student2016030421164483174";
        //得到所有的课程ID
        List<String> courseIds =new ArrayList<String>();
        //扫描器
        Scan scan = new Scan();
        //过滤器，按stuid过滤
        RowFilter rf1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(stuId+"_"));
        scan.setFilter(rf1);
        ResultScanner rs1 = table.getScanner(scan);
        Iterator<Result> it = rs1.iterator();
        while(it.hasNext()){
            Result result = it.next();
            //从表里拿到的数据是二进制数组，所以rowkey是二进制的，在第二行转为汉字
            byte[] rowKey = result.getRow();
            courseIds.add(new String(rowKey,"utf8"));
        }

        for(String id : courseIds){
            //上面得到的是整体的student2016030421164462988_course2016030421165195800" ，需要切分开
            String courseId = id.split("_")[1];
            Table  courseTable = conn.getTable(tCourse);
            Get get = new Get(courseId.getBytes());
            Result result= courseTable.get(get);
            byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());
            System.out.println("学生ID:"+stuId+" 名称："+new String("课程ID"+courseId));
        }

    }

    //查询某个课程，的学生列表 课程1
    @Test
    public void findStudentByCourse() throws IOException{
        Table table = conn.getTable(tName);
        //根据下面的课程ID查询
        String courseId="course2016030421165195800";
        //按题目要求  有多个学生，所以用数组来放
        List<String> studentIds =new ArrayList<String>();
        //设置一个扫描器
        Scan scan = new Scan();
        //过滤器
        RowFilter rf1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("_"+courseId));
        scan.setFilter(rf1);
        ResultScanner rs1 = table.getScanner(scan);
        Iterator<Result> it = rs1.iterator();
        while(it.hasNext()){
            Result result = it.next();
            byte[] rowKey = result.getRow();
            studentIds.add(new String(rowKey,"utf8"));
        }

        for(String id : studentIds){
            String stuId = id.split("_")[0];
            Table  stuTable = conn.getTable(tStudent);
            Get get = new Get(stuId.getBytes());
            Result result= stuTable.get(get);
            byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());
            System.out.println("课程ID:"+courseId+" 名字："+new String("学生ID"+stuId));
        }

    }
    //学生可以修改所选的课程  将学生1的课程1去掉
    @Test
    public void changeCourseOfStudent() throws IOException{
        String stuId="student2016030421164483174";
        String courseId="course2016030421165195800";
        String scId=stuId+"_"+courseId;
        Delete delete = new Delete(scId.getBytes());
        Table table = conn.getTable(tName);
        table.delete(delete);
    }

    @After
    public void after() throws IOException {
        if(conn!=null){
            conn.close();
        }
    }
}
/**hbase(main):008:0> scan 't_student_course'
 ROW                                                  COLUMN+CELL
 student2016030421164417190_course201603042116517244 column=cf1:student, timestamp=1546826544504, value=student2016030421164417190
 student2016030421164417190_course201603042116519580 column=cf1:student, timestamp=1546826544504, value=student2016030421164417190
 0
 student2016030421164462988_course201603042116519580 column=cf1:student, timestamp=1546826544504, value=student2016030421164462988
 0
 student2016030421164483174_course201603042116511724 column=cf1:student, timestamp=1546826544504, value=student2016030421164483174
 0
 student2016030421164483174_course201603042116517244 column=cf1:student, timestamp=1546826544504, value=student2016030421164483174
 **/